package com.wpw.dream.thread;

import java.util.concurrent.SynchronousQueue;

public class SynchronousQueueDemo {
	
	public static void main(String[] args) throws InterruptedException {
		SynchronousQueue<String> sq = new SynchronousQueue<>();
		new Thread(new TakeThread(sq)).start();
		new Thread(new PutThread(sq)).start();
	}

	
	
}
class TakeThread implements Runnable {
	
	private SynchronousQueue<String> sq;
	
	public TakeThread(SynchronousQueue<String> sq) {
		this.sq = sq;
	}
	
	@Override
	public void run() {
		try {
			for (int i = 0; i < 100; i++) {
				String take = sq.take();
				System.out.println(take);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}

class PutThread implements Runnable {
	
	private SynchronousQueue<String> sq;
	
	public PutThread(SynchronousQueue<String> sq) {
		this.sq = sq;
	}
	
	@Override
	public void run() {
		try {
			for (int i = 0; i < 100; i++) {
				sq.put(i + "");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}